package com.ruoyi.web.controller.review;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.review.domain.Comment;
import com.ruoyi.review.service.ICommentService;
import com.ruoyi.system.domain.UserComment;
import com.ruoyi.system.service.IUserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 评论管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/review/review")
public class CommentController extends BaseController
{
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserCommentService userCommentService;

    /**
     * 查询评论管理列表
     */
    @PreAuthorize("@ss.hasPermi('review:review:list')")
    @GetMapping("/list")
    public TableDataInfo list(String nickName,Integer rating,String commentName,Integer pageNum,Integer pageSiz)
    {
        startPage();
        List<Comment> list = commentService.selectCommentList(nickName,rating,commentName,pageNum,pageSiz);
        return getDataTable(list);
    }

    /**
     * 导出评论管理列表
     */
    @PreAuthorize("@ss.hasPermi('review:review:export')")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comment comment)
    {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        util.exportExcel(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('review:review:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commentService.selectCommentById(id));
    }

    /**
     * 新增评论管理
     */
    @PreAuthorize("@ss.hasPermi('review:review:add')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment)
    {
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 修改评论管理
     */
    @PreAuthorize("@ss.hasPermi('review:review:edit')")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment)
    {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 删除评论管理
     */
    @PreAuthorize("@ss.hasPermi('review:review:remove')")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commentService.deleteCommentByIds(ids));
    }
//新增评论
    @PostMapping("/addComment")
    public AjaxResult addComment(@RequestBody Comment comment){
        Long userId = SecurityUtils.getUserId();
        comment.setUserId(userId);
        //        当前时间
        LocalDateTime localDateTime = LocalDateTime.now();

        // 转换为 Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        comment.setCreateTime(date);
        comment.setUpdateTime(date);
        int i = commentService.insertComment(comment);
        if (i>0){
            Long commentId = comment.getId();
            UserComment userComment = new UserComment();
            userComment.setUserId(userId);
            userComment.setCommentId(commentId);
            return toAjax(userCommentService.insertUserComment(userComment));
        }
        return AjaxResult.error("评论失败");
    }
}
